﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Master/Default.Master" AutoEventWireup="true" CodeBehind="addRemoveClass.aspx.cs" Inherits="PointTracker.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderHead" runat="server">
    <script language="javascript" type="text/javascript">
        // Verifies that all the necessary fields for adding a class are filled in
        function VerifyFields() {
            var errors = false;
            var daySelected = false;
            var list = "";

            document.getElementById("Errors").setAttribute("style", "color:red;");
            list += "<ul>";

            if (document.getElementById("<%= TextBoxClassName.ClientID %>").value == "") {
                list += "<li>The name field cannot be empty</li>";
                errors = true;
            }
            if (document.getElementById("<%= TextBoxCRN.ClientID %>").value == "") {
                list += "<li>The CRN field cannot be empty</li>";
                errors = true;
            }
            if (document.getElementById("<%= TextBoxTerm.ClientID %>").value == "") {
                list += "<li>The term field cannot be empty</li>";
                errors = true;
            }
            if (document.getElementById("<%= TextBoxYear.ClientID %>").value == "") {
                list += "<li>The year field cannot be empty</li>";
                errors = true;
            }

            if (isNaN(document.getElementById("<%= TextBoxCRN.ClientID %>").value)) {
                list += "<li>The CRN field has to be an integer</li>";
                errors = true;
            }
            if (isNaN(document.getElementById("<%= TextBoxTerm.ClientID %>").value)) {
                list += "<li>The term field has to be an integer</li>";
                errors = true;
            }
            if (isNaN(document.getElementById("<%= TextBoxYear.ClientID %>").value)) {
                list += "<li>The year field has to be an integer</li>";
                errors = true;
            }

            var checkBoxList = document.getElementById("<%= CheckBoxListDays.ClientID %>");
            var arrayOfCheckBoxes = checkBoxList.getElementsByTagName("input");
            for (var i = 0; i < arrayOfCheckBoxes.length && !daySelected; i++) {
                daySelected = arrayOfCheckBoxes[i].checked;
            }
            if (!daySelected) {
                list += "<li>A day checkbox has to be checked</li>";
                errors = true;
            }

            // Check scribe values!!!
            if (document.getElementById("<%= CheckBoxScribe.ClientID %>").checked) {
                if (document.getElementById("<%= TextBoxScribeFname.ClientID %>").value == "") {
                    list += "<li>The scribe first name cannot be empty</li>";
                    errors = true;
                }
                if (document.getElementById("<%= TextBoxScribeLname.ClientID %>").value == "") {
                    list += "<li>The scribe last name cannot be empty</li>";
                    errors = true;
                }
                if (document.getElementById("<%= TextBoxScribeEmail.ClientID %>").value == "") {
                    list += "<li>The scribe e-mail field cannot be empty</li>";
                    errors = true;
                }
                if (document.getElementById("<%= TextBoxScribePassword.ClientID %>").value == "") {
                    list += "<li>The scribe password field cannot be empty</li>";
                    errors = true;
                }
                if (document.getElementById("<%= TextBoxScribeEmail.ClientID %>").value != "" && !echeck(document.getElementById("<%= TextBoxScribeEmail.ClientID %>").value)) {
                    list += "<li>The e-mail provided is invalid</li>";
                    errors = true;
                }
            }

            document.getElementById("Errors").innerHTML = list;

            document.getElementById("Errors").innerHTML += "</ul>";

            return !errors;
        }

        function echeck(str) {
            var at = "@"
            var dot = "."
            var lat = str.indexOf(at)
            var lstr = str.length
            var ldot = str.indexOf(dot)
            if (str.indexOf(at) == -1) {
                return false
            }

            if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
                return false
            }

            if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
                return false
            }

            if (str.indexOf(at, (lat + 1)) != -1) {
                return false
            }

            if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
                return false
            }

            if (str.indexOf(dot, (lat + 2)) == -1) {
                return false
            }
            if (str.indexOf(" ") != -1) {
                return false
            }

            return true
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderBody" runat="server">
    <fieldset>
    <legend>Add a Class</legend>
    <div id="Errors"></div>
    <table>
        <tr>
            <td style="width: 100px;">
                Class name:
            </td>
            <td style="width: 250px;">
                <asp:TextBox ID="TextBoxClassName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                CRN:
            </td>
            <td>
                <asp:TextBox ID="TextBoxCRN" MaxLength="5" runat="server"></asp:TextBox>
            </td>
            <td rowspan="3">
                <div>
                    Add a Scribe? <asp:CheckBox runat="server" ID="CheckBoxScribe" AutoPostBack="true" />
                    <table>
                        <tr>
                            <td>First Name:</td>
                            <td>
                                <asp:TextBox ID="TextBoxScribeFname" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>Last Name:</td>
                            <td>
                                <asp:TextBox ID="TextBoxScribeLname" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>E-mail:</td>
                            <td>
                                <asp:TextBox ID="TextBoxScribeEmail" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>Password:</td>
                            <td>
                                <asp:TextBox ID="TextBoxScribePassword" TextMode="Password" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </div>
            </td>
        </tr>
        <tr>
            <td valign="top">
                Days:
            </td>
            <td>
                <asp:CheckBoxList ID="CheckBoxListDays" runat="server" RepeatDirection="Vertical" TextAlign="Right">
                    <asp:ListItem Enabled="true" Text="Sunday" Value="Sunday" />
                    <asp:ListItem Enabled="true" Text="Monday" Value="Monday" />
                    <asp:ListItem Enabled="true" Text="Tuesday" Value="Tuesday" />
                    <asp:ListItem Enabled="true" Text="Wednesday" Value="Wednesday" />
                    <asp:ListItem Enabled="true" Text="Thursday" Value="Thursday" />
                    <asp:ListItem Enabled="true" Text="Friday" Value="Friday" />
                    <asp:ListItem Enabled="true" Text="Saturday" Value="Saturday" />
                </asp:CheckBoxList>
            </td>
        </tr>
        <tr>
            <td>
                Term:
            </td>
            <td>
                <asp:TextBox ID="TextBoxTerm" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Year:
            </td>
            <td>
                <asp:TextBox ID="TextBoxYear" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <asp:Button ID="ButtonAddClass" OnClick="AddClass" OnClientClick="return VerifyFields();" runat="server" Text="Add Class"/>
            </td>
        </tr>
    </table>
    </fieldset>
</asp:Content>